//
//  LikedPersonTableViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/8.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LikedPersonTableViewCell.h"
#import "TAMacro.h"
#import "DetailLikedHeaderCollectionViewCell.h"
@interface LikedPersonTableViewCell ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>

@property (nonatomic, strong) UICollectionView *likedPersonCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) UIImageView *heartImageView;
@end


@implementation LikedPersonTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)setLikeUsers:(NSArray<HomeChannelDetailLikeusers *> *)likeUsers {
    _likeUsers = likeUsers;
    [_likedPersonCollectionView reloadData];
}

- (void)createView {
    
    _heartImageView = [[UIImageView alloc] init];
    _heartImageView.image = [UIImage imageNamed:@"heart"];
    [self.contentView addSubview:_heartImageView];
    
    
    _layout = [[UICollectionViewFlowLayout alloc] init];
//    _layout.itemSize = CGSizeMake(50, 50);

    _likedPersonCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _likedPersonCollectionView.backgroundColor = [UIColor whiteColor];
    _likedPersonCollectionView.dataSource = self;
    _likedPersonCollectionView.delegate = self;
    [self.contentView addSubview:_likedPersonCollectionView];
//    [_likedPersonCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [_likedPersonCollectionView registerNib:[UINib nibWithNibName:@"DetailLikedHeaderCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DetailLikedHeaderCollectionViewCellReuse"];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (_likeUsers.count <= 16) {
        return _likeUsers.count;
    }
    return 16;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    DetailLikedHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailLikedHeaderCollectionViewCellReuse" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    cell.likeuser = _likeUsers[indexPath.row];
    return cell;
    
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    

    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 60) / 8, (SCREEN_WIDTH - 60) / 8);
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing = 0;
    
    _heartImageView.frame = CGRectMake(17, 25, 15, 15);

    
    if (_likeUsers.count == 0) {
        _likedPersonCollectionView.frame = CGRectZero;
        _heartImageView.hidden = YES;
    } else if (_likeUsers.count <= 8) {
        CGFloat height = (SCREEN_WIDTH - 60) / 8 + 20;
        _likedPersonCollectionView.frame = CGRectMake(50, 10, SCREEN_WIDTH - 60, height);
        _heartImageView.hidden = NO;

    } else {
        CGFloat height = (SCREEN_WIDTH - 60) / 4 + 20;
        _likedPersonCollectionView.frame = CGRectMake(50, 10, SCREEN_WIDTH - 60, height);
        _heartImageView.hidden = NO;

    }
    


}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
